package com.ruoyi.biz.export.executor;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruoyi.biz.category.service.IBizCategoryService;
import com.ruoyi.biz.export.vo.ReqVO;
import com.ruoyi.biz.letter.domain.BizCityLetter;
import com.ruoyi.biz.letter.export.BizCityLetterExport;
import com.ruoyi.biz.letter.service.IBizCityLetterService;
import com.ruoyi.biz.letter.util.CategoryUtil;
import com.ruoyi.common.core.domain.entity.BizCategory;
import com.ruoyi.common.enums.LetterStatus;
import com.ruoyi.common.enums.YesNoStatus;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * @author Fei.Cong
 * @date 2024-07-16 20:06
 */
@Service
public class DispatchExecutor extends AbstractExecutor {

    @Resource
    IBizCityLetterService bizCityLetterService;

    @Override
    public Map<String, Object> beforeExecute(ReqVO reqVo) {
        reqVo.getBizCityLetter().setStatusList(Lists.newArrayList(LetterStatus.CLAIM_SUCCESS.getCode(), LetterStatus.DISPATCH_SUCCESS.getCode()));
        reqVo.getBizCityLetter().setDeptId(SecurityUtils.getDeptId());
        List<BizCityLetter> cityLetters = bizCityLetterService.queryDeptList(reqVo.getBizCityLetter());

        List<BizCityLetterExport> firstVisitList = Lists.newArrayList();
        List<BizCityLetterExport> thirdVisitList = Lists.newArrayList();
        for (BizCityLetter cityLetter : cityLetters) {
            BizCityLetterExport export = new BizCityLetterExport();
            BeanUtils.copyProperties(cityLetter, export);
            export.setFirstCategoryName(CategoryUtil.getCacheCategory(cityLetter.getPCategoryId()));
            export.setSecondCategoryName(CategoryUtil.getCacheCategory(cityLetter.getSCategoryId()));
            if (Objects.equals(cityLetter.getIsVisit(), YesNoStatus.YES.getCode())) {
                firstVisitList.add(export);
            } else {
                thirdVisitList.add(export);
            }
        }
//        firstVisitList.addAll(thirdVisitList);

        Map paramMap = Maps.newHashMap();
        paramMap.put("firstVisitList", firstVisitList);
        paramMap.put("thirdVisitList", thirdVisitList);
        paramMap.put("year", reqVo.getBillMonth().split("-")[0]);
        paramMap.put("month", reqVo.getBillMonth().split("-")[1]);

        return paramMap;

    }

    @Override
    protected void afterExecute(ExcelWriter excelWriter, Map<String, Object> paramMap) {

        WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
        excelWriter.fill(paramMap.get("firstVisitList"), writeSheet);
        excelWriter.fill(paramMap, writeSheet);

        WriteSheet  writeSheet1 = EasyExcel.writerSheet(1).build();
        excelWriter.fill(paramMap.get("thirdVisitList"), writeSheet1);
        excelWriter.fill(paramMap, writeSheet1);

        // 完成写入操作
        excelWriter.finish();

    }
}
